//
//  addUserInfoVC.m
//  EJLX
//
//  Created by adc on 15/11/12.
//  Copyright © 2015年 江苏一键联信息科技有限公司. All rights reserved.
//

#import "addUserInfoVC.h"
#import "UserTableViewCell.h"
#import "SelectedCityVC.h"
@interface addUserInfoVC ()

@end

@implementation addUserInfoVC
-(void)viewWillAppear:(BOOL)animated{
    self.navigationController.navigationBarHidden = YES;
    [_tableView reloadData];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self creatNav];
    _isExit = NO;
    [self addTableView];
    [self getTheDictionaryForCity];
}


-(void)creatNav{
    UIView *navView = [[UIView alloc] initWithFrame:CGRectMake(0, 20, WIDE, 44)];
    navView.backgroundColor = RGB_COLOR(0x00c49f);
    [self.view addSubview:navView];
    
    UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    leftBtn.frame = CGRectMake(10, 7, 30, 30);
    leftBtn.layer.cornerRadius = 20;
    [leftBtn setImage:[UIImage imageNamed:@"返回箭头"] forState:UIControlStateNormal] ;
    [leftBtn addTarget:self action:@selector(backBtn) forControlEvents:UIControlEventTouchUpInside];
    [navView addSubview:leftBtn];
    
    UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(WIDE/2-60, 7, 120, 30 )];
    title.text = @"个人资料";

    title.textAlignment = NSTextAlignmentCenter;
    title.textColor = [UIColor whiteColor];
    [navView addSubview:title];
    
    UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    rightBtn.frame = CGRectMake(WIDE-50, 7, 30, 30);
    rightBtn.layer.cornerRadius = 20;
    [rightBtn setTitle:@"保存"  forState:UIControlStateNormal] ;
    rightBtn.titleLabel.font = [UIFont systemFontOfSize:12.f];
    [rightBtn addTarget:self action:@selector(save) forControlEvents:UIControlEventTouchUpInside];
    [navView addSubview:rightBtn];
    
}


-(void)backBtn{
     [self.navigationController popViewControllerAnimated:YES];
}
-(void)save{
    //上传
    static NSString *address = nil;
  
        address = [NSString stringWithFormat:@"%@,%@,%@,%@",_sheng,_shi,_qu,_address];
        
   
      // NSString *dizhi = [NSString stringWithFormat:@"%@&%@",_pickString,_address];
    [HttpsManager postPeopleInforName:_name card:_card address:address email:_email shopName:_shopName block:^(NSString *msg) {
        if ([msg isEqualToString:@"OK"]) {
            SHOW_ALERT(@"上传成功")
            if (self.returnBlock) {
                self.returnBlock(@"已完善");
            }
            [self.navigationController popViewControllerAnimated:YES];
        }else{
            SHOW_ALERT(msg)
        }
    }];
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)btnClick:(UIButton *)btn{
     if (btn.tag == 10001){
        _pickString = [NSString stringWithFormat:@"%@%@%@",_sheng,_shi,_qu];
         
    }
    [UIView animateWithDuration:0.1 animations:^{
        _pickerView.frame = CGRectMake(0, HEIGHT, WIDE, HEIGHT-56-64);
    }completion:nil];
    
    [_pickerView removeFromSuperview];
  
 [_tableView reloadData];
   
}

-(void)addTableView{
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 70, WIDE, 44*6) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.scrollEnabled = NO;
    // tableView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_tableView];
    if ([_tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        [_tableView setLayoutMargins:UIEdgeInsetsZero];
    }
    if ([_tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [_tableView setSeparatorInset:UIEdgeInsetsZero];
    }
    
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 6;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSArray *titleLB = @[@"姓名:",@"身份证:",@"地址:",@"街道:",@"邮箱:",@"店铺名:"];
    NSArray *placeholderTF = @[@"输入您的真实姓名",@"输入您的身份证号码",@"选择您所在的省份／市区",@"输入你的街道详细地址",@"输入您的有效邮箱",@"输入您的店铺名称"];
    UserTableViewCell *cell = nil;
    
        cell = [[UserTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        cell = [[NSBundle mainBundle] loadNibNamed:@"UserTableViewCell" owner:nil options:nil][0];
        
        cell.addInfoNameLB.text = titleLB[indexPath.row];
        cell.addInfoNameLB.textAlignment = NSTextAlignmentLeft;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.addInfoForPeopleTF.borderStyle = UITextBorderStyleNone;
        cell.addInfoForPeopleTF.placeholder = placeholderTF[indexPath.row];
        switch (indexPath.row) {
            case 0:
                cell.addInfoForPeopleTF.text = _name;
                break;
            case 1:
                cell.addInfoForPeopleTF.text = _card;
                break;
            case 2:
                cell.addInfoForPeopleTF.text = _pickString;
                cell.addInfoForPeopleTF.adjustsFontSizeToFitWidth = YES;
                cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
                cell.addInfoForPeopleTF.userInteractionEnabled = NO;
                break;
            case 3:
                cell.addInfoForPeopleTF.text = _address;
                break;
            case 4:
                cell.addInfoForPeopleTF.text = _email;
                break;
            case 5:
                cell.addInfoForPeopleTF.text = _shopName;
                break;
            default:
                break;
        }
        
        cell.addInfoForPeopleTF.tag = indexPath.row;
        [cell.addInfoForPeopleTF addTarget:self action:@selector(select:) forControlEvents:UIControlEventEditingDidEnd];
    
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.row ==2) {
        [_pickerView removeFromSuperview];
        _pickerView = nil;
        if (!_pickerView) {
            _pickerView = [[UIView alloc] initWithFrame:CGRectMake(0, HEIGHT, WIDE, HEIGHT-64-56)];
            
            _pickerView.backgroundColor = [UIColor lightGrayColor];
            _pickerView.alpha =0.5;
            [UIView animateWithDuration:0.1 animations:^{
                _pickerView.frame = CGRectMake(0,64, WIDE, HEIGHT-56-64);
                
                [self.view addSubview:_pickerView];
                for (int i=0; i<2; i++) {
                    UIButton *btn =[UIButton buttonWithType:UIButtonTypeCustom];
                    btn.frame = CGRectMake((WIDE/2-51)*i+50, 134, WIDE/2-51, 30);
                    NSString *str =(i==0)?@"取消":@"确定";
                    btn.backgroundColor = [UIColor whiteColor];
                    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
                    [btn setTitle:str forState:UIControlStateNormal];
                    btn.tag = 10000+i;
                    [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
                    [_pickerView addSubview:btn];
  
                }
                
                _picker = [[UIPickerView alloc] initWithFrame:CGRectMake(50,64+100, WIDE-100, (HEIGHT-56-64)/2)];
                _picker.showsSelectionIndicator = YES;
                _picker.backgroundColor = [UIColor blackColor];
                _picker.delegate = self;
                _picker.dataSource = self;
                [_pickerView addSubview:_picker];
            } ];
            
           
        }

    }
}



-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}
-(void)select:(UITextField *)TF{
   

    if (TF.tag ==0){
     _name = TF.text;
        
    }else if (TF.tag ==1){
        _card = TF.text;
    }else if (TF.tag ==3){
        _address = TF.text;
    }else if (TF.tag ==4){
        _email = TF.text;
    }else if (TF.tag ==5){
        _shopName = TF.text;
    }
  
  [_tableView reloadData];
}
-(NSMutableArray * )provinceNames{
    if(!_provinceNames){
        _provinceNames = [NSMutableArray array];
    }
    return _provinceNames;
}
-(NSMutableArray * )cityNames{
    if(!_cityNames){
        _cityNames = [NSMutableArray array];
    }
    return _cityNames;
}
-(void)getTheDictionaryForCity{
    //加载数据
    
   PraseProvince *parse = [PraseProvince manager];
    _provinces = parse.provinces;
    
    
    for (NSString* pn in _provinces.allKeys) {
        [self.provinceNames addObject:pn];
    }
    
    NSString * provinceName = _provinceNames[0];
    _cities = _provinces[provinceName];
    for (NSString * cn in _cities.allKeys) {
        [self.cityNames addObject:cn];
    }
    _areas = _cities[_cityNames[0]];
    
}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 3;
}
-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
    UILabel *pickLB = (UILabel *)view;
    if (!pickLB) {
        pickLB = [[UILabel alloc] init];
        pickLB.adjustsFontSizeToFitWidth = YES;
        pickLB.textAlignment = NSTextAlignmentCenter;
        [pickLB setBackgroundColor:RGB_COLOR(0x00c49f)];
        [pickLB setFont:[UIFont boldSystemFontOfSize:20]];
    }
    pickLB.text = [self pickerView:pickerView titleForRow:row forComponent:component];
    return pickLB;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    
    
    switch (component) {
        case 0:
            return [_provinceNames count];
            break;
        case 1:
            return [_cities count];
            break;
        case 2:
            return [_areas count];
            break;
            
        default:
            return 0;
            break;
    }
    
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    
    switch (component) {
        case 0:
            _sheng = _provinceNames[row] ;
            return _provinceNames[row] ;
            
            break;
        case 1:
            _shi = _cityNames[row];
            return _cityNames[row];
            break;
        case 2:
            _qu = _areas.allKeys[row];
            return _areas.allKeys[row];
        default:
            return  @"";
            break;
    }
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    switch (component) {
        case 0:{
            _sheng = [_provinceNames objectAtIndex:row] ;
            
            _cities = [_provinces objectForKey:_sheng];
            self.cityNames = nil;
            for (NSString * cn in _cities.allKeys) {
                [self.cityNames addObject:cn];
            }
            [self.picker selectRow:0 inComponent:1 animated:YES];
            [self.picker reloadComponent:1];//更新第二个数据
            
            NSString * cityName = _cityNames.firstObject;
            _areas = _cities[cityName];//objectForKey:@"areas"];
            
            [self.picker selectRow:0 inComponent:2 animated:YES];
            [self.picker reloadComponent:2];
        }
            break;
        case 1:
            _areas = _cities[_cityNames[row]] ;// objectForKey:@"areas"];
            [self.picker selectRow:0 inComponent:2 animated:YES];// index
            [self.picker reloadComponent:2];
            break;
        default:
            break;
    }
    //    if ([self.delegate respondsToSelector:@selector(pickerDidChaneStatus:)]) {
    //        [self.delegate pickerDidChaneStatus:self];
    //    }
}

//-(void)getTheDictionaryForCity{
//    //加载数据
//   _provinces = [[NSArray alloc]initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"area.plist" ofType:nil]];
//    _cities = [[_provinces objectAtIndex:0] objectForKey:@"cities"];
//    _sheng = [[_provinces objectAtIndex:0]objectForKey:@"state"];
//    _shi = [[_cities objectAtIndex:0] objectForKey:@"city"];
//    
//    _areas = [[_cities objectAtIndex:0] objectForKey:@"areas"];
//    if (_areas.count > 0) {
//        _qu = [_areas objectAtIndex:0];
//    } else{
//        _qu = @"";
//    }
//}
//
//- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
//{
//    return 3;
//}
//-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
//    UILabel *pickLB = (UILabel *)view;
//    if (!pickLB) {
//        pickLB = [[UILabel alloc] init];
//        pickLB.adjustsFontSizeToFitWidth = YES;
//        
//        pickLB.textAlignment = NSTextAlignmentCenter;
//        [pickLB setBackgroundColor:[UIColor clearColor]];
//        [pickLB setFont:[UIFont boldSystemFontOfSize:20]];
//    }
//    pickLB.text = [self pickerView:pickerView titleForRow:row forComponent:component];
//    return pickLB;
//}
//- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
//{
//    switch (component) {
//        case 0:
//            return [_provinces count];
//            break;
//        case 1:
//            return [_cities count];
//            break;
//        case 2:
//            return [_areas count];
//            break;
//            
//        default:
//            return 0;
//            break;
//    }
//    
//}
//
//- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
//{
//    
//    switch (component) {
//        case 0:
//            return [[_provinces objectAtIndex:row] objectForKey:@"state"];
//            break;
//        case 1:
//            return [[_cities objectAtIndex:row] objectForKey:@"city"];
//            break;
//        case 2:
//            if ([_areas count] > 0) {
//                return [_areas objectAtIndex:row];
//                break;
//            }
//        default:
//            return  @"";
//            break;
//    }
//}
//
//- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
//{
//   // [pickerView selectRow:2 inComponent:0 animated:NO];
//    
//    switch (component) {
//        case 0:
//            
//            _sheng = [[_provinces objectAtIndex:row] objectForKey:@"state"];
//            
//            _cities = [[_provinces objectAtIndex:row] objectForKey:@"cities"];
//            [self.picker selectRow:0 inComponent:1 animated:YES];
//            [self.picker reloadComponent:1];//更新第二个数据
//            
//            _areas = [[_cities objectAtIndex:0] objectForKey:@"areas"];
//            [self.picker selectRow:0 inComponent:2 animated:YES];
//            [self.picker reloadComponent:2];
//        _shi = [[_cities objectAtIndex:0] objectForKey:@"city"];
//            
//       if ([_areas count] > 0) {
//                _qu= [_areas objectAtIndex:0];
//            } else{
//                _qu = @"";
//            }
//           break;
//      
//        case 1:
//            _areas = [[_cities objectAtIndex:row] objectForKey:@"areas"];
//            [self.picker selectRow:0 inComponent:2 animated:YES];// index
//            [self.picker reloadComponent:2];
//            
//           _shi = [[_cities objectAtIndex:row] objectForKey:@"city"];
//            if ([_areas count] > 0) {
//                _qu = [_areas objectAtIndex:0];
//          } else{
//                _qu = @"";
//            }
//            break;
//        case 2:
//            if ([_areas count] > 0) {
//                _qu = [_areas objectAtIndex:row];
//              
//            } else{
//                _qu = @"";
//            }
//            break;
//        default:
//            break;
//    }
//    
//}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
